<?php
function truyenf1_get_truyen($id){
	$cache=new cache;
	$data=$cache->load($id,'truyen/'.$id,1);
	if(!$data){
		$db=new Model2;
	
		$db->query('select truyen.* from truyen
				 	where truyen.id='.intval($id));
		if($db->num_rows()==0){
			return false;
		//	header('HTTP/1.0 404 Not Found');
		//	redirect(HOME.'404.html');
		//	die();

		}
					
		$data=$db->fetchOne();
		if($data['tinhtrang']==1){
			$data['tinhtrang-text']="<a href='".HOME.TRUYEN."/status/truyen-hoan-thanh.html'>Hoàn Thành</a>";
		}else
		if($data['tinhtrang']==2){
			$data['tinhtrang-text']= "<a href='".HOME.TRUYEN."/status/truyen-dang-cap-nhat.html'>Đang Tiến Hành</a>";
		}else{
			$data['tinhtrang-text']= "<a href='".HOME.TRUYEN."/status/truyen-tam-ngung.html'>Tạm Ngưng</a>";
		}
		
		$data['tag']=truyenf1_truyen_tag_link($data['tags']);
		
	
		
		// Tim link chuong moi nhat, userupdate,time update,tongsochuong hien tai
		$db->query('select id,ten,alias,time_upload,user_upload from truyen_chuong where truyen_id='.$id.' order by thutu desc limit 0,1');
		if($db->num_rows()>0){
			$dt=$db->fetchOne();
			$c_url=truyenf1_url_truyen_chuong($dt['id'],$dt['alias'],$data['alias']);
			$db->nonquery('update truyen set chuongmoinhat="'.$dt['ten'].'",chuongmoinhat_url="'.$c_url.'",time_update="'.$dt['time_upload'].'",user_update="'.$dt['user_upload'].'" where id='.$id);
		}
		
		
		$data['chuongmoinhat']=$dt['ten'];
		$data['chuongmoinhat_url']=$c_url;
		$data['user_update']=$dt['user_upload'];
		$data['time_update']=$dt['time_upload'];
	
		//Tìm tổng số chương
		 $db->query(" select count(*) as num from truyen_chuong where truyen_id=".$id);
		 $dt2=$db->fetchOne();
		 $data['sochuong']=$dt2['num'];
		 
		 //Tim chuong bat dau
		 $db->query('select id,ten,alias from truyen_chuong where truyen_id='.$id.' order by thutu asc limit 0,1');
		 $dt=$db->fetchOne();
		 $data['chuongbatdau']=$dt['ten'];
		 $data['chuongbatdau_url']=truyenf1_url_truyen_chuong($dt['id'],$dt['alias'],$data['alias']);
		 
		
		 // Update table the loai cai coi
		 $sql="update truyen_theloai_fk set views=".$data['views'].",likes=".$data['likes'].",comments=".$data['comments'].",time_upload='".$data['time_upload']."',tinhtrang=".$data['tinhtrang'].",time_update='".$data['time_update']."',tongsochuong=".$data['sochuong'].",view_week=".$data['view_week'].",view_month=".$data['view_month']." where truyen_id=".$id ;
		// echo $sql;
		$db->nonquery($sql);
		
		//Add truyen tag neu gtag=0, trong khi add se update gtag=1
		$gtags=truyenf1_truyen_tag_generate($data['ten']);
		if($data['gtags']==0){
		
			truyenf1_truyen_tag_add($gtags,$data['id']);
			
			
		}
	
		
		$data['gtags']=truyenf1_truyen_tag_link($gtags);
		$data['theloai2']=truyenf1_truyen_get_theloai($data['theloai']);
		
		
		$cache->save($id,$data,86000,'truyen/'.$id,1);
	}
	return $data;
}

function truyenf1_truyen_get_theloai($str){
		
		$theloaiar=explode(",",$str);
		$str='';
		$str2='';
			$db2=new Model2;
			foreach($theloaiar as $value ){
				$value=intval($value);
				$db2->query('select alias,ten from truyen_theloai where id='.$value);
				$dt=$db2->fetchOne();
				$str.="<a href='".truyenf1_url_truyen_theloai($value,$dt['alias'])."'> <i class='fa fa-tags'></i> ".$dt['ten']." </a> ";
				$str2.="<a href='".truyenf1_url_truyen_theloai($value,$dt['alias'])."'  itemprop='url'> <span itemprop='title'>".$dt['ten']."</span></a> ,";
										
			}
			$str=substr($str,0,strlen($str)-1);
			$str2=substr($str2,0,strlen($str2)-1);
		$db2->close();
		$row['1']=$str;
		$row['2']=$str2;
		return $row;
}

function truyenf1_truyen_get_chuong_list($id,$page){
	$start=($page-1)*30;
	//$cache=new cache;
	//$data=$cache->load($page,'truyen/'.$id.'/chuong_list');
	if(!$data){
		$db=new Model2;
	
		$db->query('select id,ten,alias,thutu,views,time_upload,user_upload from truyen_chuong where trangthai=1 and xoa=0 and truyen_id='.$id.' order by thutu desc limit '.$start.',30');
		$data=$db->fetchAll();
		
		//$cache->save($page,$data,86000,'truyen/'.$id.'/chuong_list');
		}
	return $data;
}

function truyenf1_truyen_get_chuong($id){

	if(!$data){
		$table=table_chuong($id);
		$db=new Model2;
	
		$db->query('select * from truyen_chuong where id='.intval($id));
		if($db->num_rows()==0){
			//header('HTTP/1.0 404 Not Found');
			//redirect(HOME.'404.html');
			//	die();
			return false;

		}
		
		$data=$db->fetchOne();
		$db->query('select * from '.$table.' where truyen_chuong_id='.intval($id));
		$data2=$db->fetchOne();
		$data['noidung']=$data2['noidung'];
		
		
		// GET LINK NEXT AND PREVIOUS
		$db->query('select id,ten,alias from truyen_chuong where truyen_id='.$data['truyen_id'].' and thutu <'.$data['thutu']." order by thutu desc limit 0,1");
		if($db->num_rows()>0){
		$data['previous']=$db->fetchOne();
		
		}else{
		$data['previous']='';	
		}
		
		$db->query('select id,ten,alias from truyen_chuong where truyen_id='.$data['truyen_id'].' and thutu >'.$data['thutu']." order by thutu asc limit 0,1");
		if($db->num_rows()>0){
		$data['next']=$db->fetchOne();
			
		}else{
		$data['next']='';	
		}
		//
		
	//	$cache->save($id,$data,8600,'truyen/chuong');
		$db->close();
		
		}
	return $data;
}


function truyenf1_truyen_theloai_get_list_desktop(){
	$cache=new cache;
	$str=$cache->load('list_truyen_category','truyen',1);
	if(!$str){
	 $db=new Model2;

	 $db->query('select * from truyen_theloai  order by thutu asc');
	 $dt=$db->fetchAll();
	
	 $data=array();
	 	foreach($dt as $row){
			
				$row['url']=url_truyen_theloai($row['id'],$row['alias']);
			
			$data[]=$row;
		}
		$row['url']=HOME.TRUYEN."/status/truyen-hoan-thanh.html";
		$row['ten']="Truyện Full";
		$data[]=$row;
		$row['url']=HOME.TRUYEN."/status/truyen-moi.html";
		$row['ten']="Truyện Mới";
		$data[]=$row;

		$row['url']=HOME.TRUYEN."/status/truyen-hay.html";
		$row['ten']="Truyện Hay";
		$data[]=$row;
		$row['url']=HOME.TRUYEN."";
		$row['ten']="Mới Cập Nhật";
			
		$data[]=$row;
		
	
	$str="<table class='table  table_truyen_theloai'>";
	$j=1;
	$i=1;
	$color=array('#c0392b','#e84c3d','#e67e22','#f1c40f','#2ecc71','#27ae60','#1abc9c','#16a085','#3498db','#2980b9','#c0392b','#e84c3d');
	foreach($data as $row){
		if($j%2==1){
			$str.="<tr style='background-color:".$color[$i]."'><td><i class='fa fa-tags'></i><a href='".$row['url']."' title='".$row['ten']."'>".$row['ten']."</a></td>";
			$i++;
		}
		if($j%2==0){
			$str.="<td><i class='fa fa-tags'></i><a href='".$row['url']."' title='".$row['ten']."'>".$row['ten']."</a></td></tr>";
		}
		$j++;
	}

	if(($j-1)%2==1){
		$str.="<td>&nbsp;</td></tr>";
	}
	
	
	$str.="</table>";
	
		
	$cache->save('list_truyen_category',$str,3600,'truyen',1);	
	}
	return $str;
	
}




function truyenf1_truyen_get_view(){
	$cache=new cache;
	$str=$cache->load('get_truyen_view','truyen',1);
	if(!$str){
		$db=new Model2;
		$db->connect();
		$db->query('select id,ten,alias,views,hinhanh,chuongmoinhat,chuongmoinhat_url,tinhtrang from truyen order by views desc limit 0,10');
		
		$str= "<div class='list-group'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
				$j=1;
			foreach($dt as $row){
							if($j==1){
								$str.=" <a href='".truyenf1_url_truyen($row['id'],$row['alias'])."' class='list-group-item top_video_item'>
									<p class='box-news-image'><img src='".TRUYEN_IMAGE_LOCAL.$row['hinhanh']."' /></p>".$row['ten']."  <br /><span class='view'>".$row['view_day']." Lượt xem</span>
						<p class='chuong'> ".$row['chuongmoinhat']."</p></a>";  
							}else{
								 $str.="<a href='".truyenf1_url_truyen($row['id'],$row['alias'])."' class='list-group-item'>".$row['ten']."<span class='view pull-right'>".$row['views']." Lượt xem</span><div class='clearfix'></div></a>"; 
							}
							$j++;
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_truyen_view',$str,1800,'truyen',1);
	} // End !$data
	
	return $str;

}// End function get truyen view

function truyenf1_truyen_get_view_day(){

	$cache=new cache;
	$str=$cache->load('get_truyen_view_day','truyen',1);

	if(!$str){

		$db=new Model2;
	
		$db->query("select id,ten,alias,view_day,chuongmoinhat,chuongmoinhat_url,hinhanh,tinhtrang from truyen where last_update_view='".date('Y-m-d')."' order by view_day desc limit 0,10");

		$str= "<div class='list-group'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
			$j=1;
			foreach($dt as $row){
				if($j==1){
					$str.="<a href='".truyenf1_url_truyen($row['id'],$row['alias'])."' class='list-group-item top_video_item top_truyen_bg'>
									<p class='box-news-image'><img width='60' height='94' src='".TRUYEN_IMAGE_LOCAL.$row['hinhanh']."' /></p>".$row['ten']."  <br /><span class='view'>".$row['view_day']." Lượt xem</span>
						<p class='chuong'>".$row['chuongmoinhat']."</p></a>";
				}else{

					$str.="<a href='".truyenf1_url_truyen($row['id'],$row['alias'])."' class='list-group-item'><span class='medal'>".$j."</span>".$row['ten']."<span class='view pull-right'>".$row['view_day']." Lượt xem</span></a>";
				}
				$j++;

			} // End foreach
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_truyen_view_day',$str,1800,'truyen',1);
	} // End !$data

	return $str;

} // End of function get truyen view day



function truyenf1_truyen_get_view_week(){
	$cache=new cache;
	$str=$cache->load('get_truyen_view_week','truyen',1);
	if(!$str){
		$db=new Model2;
		$db->connect();
		$db->query("select id,ten,alias,view_week,hinhanh,chuongmoinhat,chuongmoinhat_url,tinhtrang from truyen where WEEK(last_update_view, 3) = '" . date('W') . "' order by view_week desc limit 0,10");
		
	$str= "<div class='list-group'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
			$j=1;
			foreach($dt as $row){
				if($j==1){
									$str.=" <a href='".truyenf1_url_truyen($row['id'],$row['alias'])."' class='list-group-item top_video_item'>
									<p class='box-news-image'><img src='".TRUYEN_IMAGE_LOCAL.$row['hinhanh']."' /></p>".$row['ten']."  <br /><span class='view'>".$row['view_day']." Lượt xem</span>
						<p class='chuong'>".$row['chuongmoinhat']."</p></a>"; 
							}else{
								 $str.="<a href='".truyenf1_url_truyen($row['id'],$row['alias'])."' class='list-group-item'>".$row['ten']."<span class='view pull-right'>".$row['view_week']." Lượt xem</span><div class='clearfix'></div></a>"; 
					}
								 $j++;
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_truyen_view_week',$str,1800,'truyen',1);
	} // End !$data
	
	return $str;

} // End of function get truyen view week


function truyenf1_truyen_get_view_month(){
	$cache=new cache;
	$str=$cache->load('get_truyen_view_month','truyen',1);
	if(!$str){
		$db=new Model2;
		$db->query("select id,ten,alias,view_month,hinhanh,chuongmoinhat,chuongmoinhat_url,tinhtrang from truyen where MONTH(last_update_view) = '" . date('m') . "' order by view_month desc limit 0,10");
		
		$str= "<div class='list-group'>";
		if($db->num_rows()>0){
			$dt=$db->fetchAll();
			$j=1;
			foreach($dt as $row){
					if($j==1){
									$str.=" <a href='".truyenf1_url_truyen($row['id'],$row['alias'])."' class='list-group-item top_video_item'>
									<p class='box-news-image'><img src='".TRUYEN_IMAGE_LOCAL.$row['hinhanh']."' /></p>".$row['ten']."  <br /><span class='view'>".$row['view_day']." Lượt xem</span>
						<p class='chuong'>".$row['chuongmoinhat']."</p></a>"; 
							}else{
								 $str.="<a href='".truyenf1_url_truyen($row['id'],$row['alias'])."' class='list-group-item'>".$row['ten']."<span class='view pull-right'>".$row['view_month']." Lượt xem</span><div class='clearfix'></div></a>"; 
							}
							$j++;
				
			} // End foreach 
		}else{// End num row >0
			echo "<h1 class='text-center alert alert-warning'>Chưa có dữ liệu</h1>";
		}
		$str.='</div>';
		$cache->save('get_truyen_view_month',$str,1800,'truyen',1);
	} // End !$data
	
	return $str;

} // End of function get truyen view month




function cache_get_truyen_comment(){
	$cache=new cache;
	$str=$cache->load('list_truyen_comment','truyen',1);
	if(!$str){
		// -- Get 10 comment moi nhat
		$db=new Model2;
		
		$db->query("select * from comment where type='truyen' order by id desc limit 0,6");
		if($db->num_rows()>0){
			$data=$db->fetchAll();
              $str="<div class='list-group' id='right-comment-list'>";
              $j=1;
               foreach($data as $row){
                 	if(0){
               			
                    }else{ // End $j==1
                              $str.="<a title='".$row['tieude']."' href='".$row['url']."' class='list-group-item'><span class='tieude'>".cutstring($row['tieude'], 40)."</span><span class='view pull-right'>".time_ago($row['time_add'])." </span><div class='clearfix'></div><div class='sdes'>".cutstring($row['noidung'], 100)."</div></a>";
                      }
					  $j++;
                   } // End foreach

			$str.="</div>";
           
			
			
			
			
			
			
		}else{
			$str= "<h1 class='text-center'>Không Có Dữ Liệu</h1>"	;
		}
		
		
		$cache->save('list_truyen_comment',$str,600,'truyen',1);
	}
	
	return $str;

} //---------End truyen function -------


//-------------------------- Mobile Function
function truyenf1_truyen_theloai_get_lists_mobi(){
	$cache=new cache;
	$str=$cache->load('list_truyen_category_mobi','truyen',1);
	if(!$str){
	 $db=new Model2;
	 $db->query('select * from truyen_theloai  order by ten asc');
	 $dt=$db->fetchAll();
	
	 $data=array();
	 $row['url']=HOME.TRUYEN."/status/truyen-hoan-thanh.html";
		$row['ten']="Truyện Full";
		$data[]=$row;
		$row['url']=HOME.TRUYEN."/status/truyen-moi.html";
		$row['ten']="Truyện Mới ";
		$data[]=$row;
		$row['url']=HOME.TRUYEN."";
		$row['ten']="Mới Cập Nhật";
		$data[]=$row;
		$row['url']=HOME.TRUYEN."/status/truyen-hay.html";
		$row['ten']="Truyện Hay";
		$data[]=$row;
	
	 	foreach($dt as $row){
			$row['url']=truyenf1_url_truyen_theloai($row['id'],$row['alias']);
			$data[]=$row;
		}
		$str.="<li><a href='".HOME.TRUYEN."' title='".$row['ten']."'><span aria-hidden='true' class='icon-home'></span>Kho Truyện</a></li>";
		foreach($data as $row){
			$str.="<li><a href='".$row['url']."' title='".$row['ten']."'><span aria-hidden='true' class='icon-minus'></span>".$row['ten']."</a></li>";
	 }
	$cache->save('list_truyen_category_mobi',$str,3600,'truyen',1);	
	}
	return $str;
	
}
function truyenf1_truyen_theloai_get_list2_mobi2(){
	$cache=new cache;
	$str=$cache->load('list_truyen_category_mobi2','truyen',1);
	if(!$str){
	 $db=new Model2;
	 $db->connect();
	 $db->query('select * from truyen_theloai  order by ten asc');
	 $dt=$db->fetchAll();
	
	 $data=array();
	 	foreach($dt as $row){
			$row['url']=truyenf1_url_truyen_theloai($row['id'],$row['alias']);
			$data[]=$row;
		}
		foreach($data as $row){
			$str.="<li><a href='".$row['url']."' title='".$row['ten']."'>".$row['ten']."</a></li>";
	 }
	$cache->save('list_truyen_category_mobi2',$str,3600,'truyen',1);	
	}
	return $str;
	
}

